<?php
/**
 * @version $Id: view.html.php 48 2011-09-28 06:47:49Z lnmtrung $
 */
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );

class WrdViewExchange extends WrdView{
    
    function display($tpl = null) {
        
        $this->setModel($this->getModel('exchange'), true);
        $fnc = 'display'.ucfirst($this->getLayout());
        if (is_callable(array(&$this, $fnc)))
            $this->$fnc($tpl);
        else
            parent::display($tpl);
    }

    function displayDefault($tpl = null){
        
        $mainframe = &JFactory::getApplication();
        $context = JRequest::getCmd('option');
        $view = JRequest::getCmd('view');
        
        $document = & JFactory::getDocument();
        $document->setTitle( JText::_('Exchange') );

        JToolBarHelper::title(JText::_('Exchange'),'cpoint.png');
        JToolBarHelper::addNew();
        JToolBarHelper::deleteList();
        
        WrdHelper::addSubMenu('config',$view);
		$this->sidebar = JHtmlSidebar::render();

        $filter_order     		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_item','filter_order','id' );
        $filter_order_Dir 		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir','filter_order_Dir', '' );

        $lists['order'] 		= $filter_order;
        $lists['order_Dir'] 	= $filter_order_Dir;
        
        $items					= & $this->get( 'Data');
        $user_info = array() ;
        foreach ($items as $i){
            $user_info[$i->user_id] = WrdHelper::getUserProfile($i->user_id, 'userid') ;
        }
        
        $total					= & $this->get( 'Total');
        $pagination 			= & $this->get( 'Pagination' );
         
        $search 				= $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string' );
        $filter_state 			= $mainframe->getUserStateFromRequest( $context.$view.'published','published',-1,'int' );
        $filter_user			= $mainframe->getUserStateFromRequest( $context.$view.'created_id','created_id',-1,'int' );
        
        $search 				= JString::strtolower( $search );
        
        $lists['search'] 		= $search;

        $this->assignRef('search',		$search);
        $this->assignRef('lists',		$lists);
        $this->assignRef('items',		$items);
        $this->assignRef('user_info',	$user_info);
        $this->assignRef('pagination',	$pagination);
        
        parent::display($tpl);
    }
  
    function displayForm($tpl = null){
        
        $mainframe = &JFactory::getApplication();
        $context = JRequest::getCmd('option');
        $view = JRequest::getCmd('view');
        $add = JRequest::getVar('task');
        
        $user = JFactory::getUser() ;
        $cid = JRequest::getInt('id');
        $model	=& $this->getModel();
        $vip_num = JRequest::getInt('vip_num');
        $model_outlet = &JModelLegacy::getInstance('outlets','WrdModel');
        $detail = array();

        if(empty($vip_num)){
            if($cid != 0){
                $detail	=& $model->getItem($cid);
                $u_info = WrdHelper::getUserProfile($detail->user_id,'userid') ;
                $forward = 0;
            }
        }else{
            $u_info = WrdHelper::getUserProfile($vip_num,'member_id') ;
            $forward = 1;
        }
        
        if(!empty($u_info->user_id)){
            $outlet = $model_outlet->getItem($u_info->pick_up_at);
            $p_o_u = WrdHelper::getPoints($u_info->user_id);
            if($u_info->job != 'other'){
                $job = $this->getJobName($u_info->job) ;
            }else{
                $job = $u_info->o_job ;
            }
        }
        
        $text = ($detail->id < 1) ? JText::_( 'NEW' ) : JText::_( 'EDIT' );
        //var_dump($text);
        JToolBarHelper::title(JText::_('Exchange').': <small><small>['.$text.']</small></small>','cpoint.png' );
        JToolBarHelper::save();
        JToolBarHelper::cancel( 'cancel', 'Close' );
        $this->assignRef('add',	$add);
        $this->assignRef('data',$detail);
        $this->assignRef('user',	$user);
        $this->assignRef('u_info',	$u_info);
        $this->assignRef('p_o_u',	$p_o_u);
        
        $this->assignRef('outlet',	$outlet);
        $this->assignRef('job',$job);
        $this->assignRef('forward',$forward);
        parent::display($tpl);
    }
    
    function getJobName($id = 0){
        $db = JFactory::getDbo();
        $query = 'SELECT name FROM #__wrd_job WHERE id ='.$id;
        $db->setQuery($query);
        $rs = $db->loadObject();
        return $rs->name;
    }
}
?>
